package com.pig4cloud.pig.upload.util;

import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;

public class OssUtil {

    // endpoint以杭州为例，其它region请按实际情况填写。
    private static final String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，
    // 请登录 https://ram.console.aliyun.com 创建RAM账号。
    // 创建OSSClient实例。

	private static final String accessKeyId = "LTAI4FrTuvWZ34WoiG6Qdhx6";
	private static final String accessKeySecret = "7fUoqdQm6SULUKxI9LO8Bjpd0cNRaK";
	private static final String bucketName = "govern-yinan";
	public static final String httpUrl = "http://govern-yinan.oss-cn-beijing.aliyuncs.com/";

    public static final int TYPE_USER_PHOTOS = 1;//用户头像
    public static final int TYPE_PRODUCT_SKU_IMGS  = 2;// 商品的sku图片
    public static final int TYPE_PRODUCT_IMGS  = 3;// 商品的sku图片
    public static final int TYPE_PRODUCT_VIDEOS  = 4;// 商品的视频
    public static final int TYPE_SIMPLE_IMAGE  = 5;// 商品的视频
    public static final int TYPE_ORDER_APPRAISE_IMGS  = 6;// 商品的sku视频文件
    public static final int TYPE_EVENT_IMGS  = 7;// 商品的sku视频文件

    private static final String USER_PHOTOS_PREFIX = "user/photos/";
    private static final String PRODUCT_SKU_IMG = "product/sku/imgs/";
    private static final String PRODUCT_IMG = "product/imgs/";
    private static final String PRODUCT_VIDEO = "product/videos/";
    private static final String SIMPLE_IMAGE = "simple/image/";
    private static final String ORDER_APPRAISE_IMGS = "order/appraise/imgs/";
    private static final String EVENT_IMGS = "event/imgs/";

    private static final int validity = 3600 * 1000;

    public static void main(String args[]){
        //upload();
    }

    /**
     * 上传文件
     * @param inputStream 要上传的文件流
     * @param fileName 要上传的文件名字
     * @param upload_type 要上传的文件类型
     */
    public static boolean upload(InputStream inputStream, String fileName, int upload_type) {
        boolean ret = false;
        // 上传文件流
        if (inputStream!=null){
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//            String prefix = FileUtil.getUploadPrefix(upload_type);
			PutObjectResult putObjectResult = ossClient.putObject(bucketName, fileName, inputStream);
			String eTag = putObjectResult.getETag();
			System.out.println("upload "+fileName +"-->"+ eTag);
			if(StrUtil.isNotEmpty(eTag)){
				ret = true;
			}
            // 关闭client。
            ossClient.shutdown();
        }
        System.out.println("upload "+fileName +"是否成功-->"+ (ret?"YES":"NO"));
        return ret;
    }

    /**
     * 根据key获取url
     * @param object_key
     * @return
     */
    public static String getSignUrl(String object_key){
        //创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId,accessKeySecret);
        //validity url的有效期
        URL url = ossClient.generatePresignedUrl(bucketName, object_key, new Date(new Date().getTime()+validity));;
        //关闭client
        ossClient.shutdown();
        return url==null?null:url.toString();
    }


    /**
     * 删除文件
     * @param objKey
     * @return
     */
    public static boolean deleteObjByKey(String objKey) {
        boolean ret = false;
        //创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId,accessKeySecret);
        ossClient.deleteObject(bucketName,objKey);
        // Object是否存在。
        ret = ossClient.doesObjectExist(bucketName, objKey);
        ossClient.shutdown();
        return !ret;
    }

    /**
     * 获取  上传图片的 路径前缀
     * @param upload_type
     * @return
     */
    public static String getUploadPrefix(int upload_type){
        String prefix = null;
        switch (upload_type) {
            case TYPE_USER_PHOTOS:
                prefix = USER_PHOTOS_PREFIX;
                break;
            case TYPE_PRODUCT_SKU_IMGS:
                prefix = PRODUCT_SKU_IMG;
                break;
            case TYPE_PRODUCT_IMGS:
                prefix = PRODUCT_IMG;
                break;
            case TYPE_PRODUCT_VIDEOS:
                prefix = PRODUCT_VIDEO;
                break;
            case TYPE_SIMPLE_IMAGE:
                prefix = SIMPLE_IMAGE;
                break;
            case TYPE_ORDER_APPRAISE_IMGS:
                prefix = ORDER_APPRAISE_IMGS;
                break;
            case TYPE_EVENT_IMGS:
                prefix = EVENT_IMGS;
                break;
        }
        return prefix;
    }
}
